import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/channel.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/multi_state_widget/multi_controller.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/print_detail/device_detail/device_detail_controller.dart';

class DeviceDetailPage extends StatefulWidget {
  const DeviceDetailPage({Key? key}) : super(key: key);

  @override
  _DeviceDetailPageState createState() => _DeviceDetailPageState();
}

class _DeviceDetailPageState
    extends BaseState<DeviceDetailPage, DeviceDetailController> {
  final MultiController multiController = MultiController();

  MethodChannel fromAndroidChannel =
      const MethodChannel("DeviceDetailPageFlutter");

  String equipmentModel = '';

  String macAddress = '';

  @override
  void onCreate() {
    setTitle(MultiLanguage.add_printer.tr);
    multiController.initStatus = MultiStateStatus.loading;
    sendAndroidChannel.invokeMethod(DEVICE_INFO);
    fromAndroidChannel.setMethodCallHandler(flutterMethod);
  }

  Future<dynamic> flutterMethod(MethodCall methodCall) async {
    switch (methodCall.method) {
      case DEVICE_INFO:
        var deviceInfo =
            json.decode(methodCall.arguments) as Map<String, dynamic>;
        equipmentModel = deviceInfo['name']!;
        macAddress = deviceInfo['address']!;
        break;
    }
    setSafeState();
  }

  @override
  DeviceDetailController registerController() =>
      Get.find<DeviceDetailController>();

  @override
  Widget? getMainWidget(BuildContext context) {
    return Column(
      children: [
        Container(
          height: 0.3.sh,
          alignment: Alignment.center,
          child: LocalImage(
            'common_printer01',
            package: Constant.packLib(),
            width: 300.w,
            height: 288.w,
          ),
        ),
        Expanded(
          child: Container(
            padding: edgeLR24,
            margin: edgeLR24,
            decoration: BoxDecoration(
              color: ThemeUtil.getTheme().colorScheme.secondary,
              border: Border.all(
                  color: ThemeUtil.getTheme().dividingLineColor(),
                  width: 1.0),
              borderRadius: const BorderRadius.all(Radius.circular(8)),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.1), // 阴影的颜色
                  offset: const Offset(0, 0), // 阴影与容器的距离
                  blurRadius: 5.0, // 高斯的标准偏差与盒子的形状卷积。
                  spreadRadius: 0.0, // 在应用模糊之前，框应该膨胀的量。
                ),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                gapH18,
                Row(children: [
                  LocalImage('common_connect_blue',
                      package: Constant.packLib(), width: 40.w, height: 40.w),
                  gapW18,
                  Text(MultiLanguage.connected.tr)
                ]),
                gapH18,
                Row(children: [
                  Text("${MultiLanguage.equipment_model.tr}:  "),
                  Text(equipmentModel),
                ]),
                gapH18,
                line1,
                gapH18,
                Row(children: [
                  Text("${MultiLanguage.mac_address.tr}:  "),
                  Text(macAddress),
                ]),
                gapH18,
                line1,
                gapH18,
                Expanded(
                  child: Container(
                    alignment: Alignment.bottomCenter,
                    child: GradientButton(
                      ThemeUtil.getTheme(),
                      margin: edgeLR24 as EdgeInsets,
                      padding: edgeV24 as EdgeInsets,
                      width: double.infinity,
                      text: MultiLanguage.disconnect.tr,
                      onPressed: () {
                        sendAndroidChannel.invokeMethod(DISSMISS_CONNECT);
                        Get.back();
                      },
                    ),
                  ),
                ),
                gapH40,
                gapH40,
              ],
            ),
          ),
        ),
        gapH40,
      ],
    );
  }
}
